package org.example.backend.demos.web.ManagementModule.controller;

import org.example.backend.demos.web.ManagementModule.entity.Project;
import org.example.backend.demos.web.ManagementModule.service.ProjectService;
import org.example.backend.demos.web.api.projectsApi;
import org.example.backend.demos.web.result.Pages;
import org.example.backend.demos.web.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;


/**
 * 项目表(Project)表控制层
 *
 * @author makejava
 * @since 2025-09-08 14:58:59
 */
@RestController
@RequestMapping("projects")
public class ProjectController implements projectsApi {
    /**
     * 服务对象
     */
    @Autowired
    private ProjectService projectService;

    /**
     * 获取所有项目
     *
     * @return 查询结果
     */
    @Override
    public ResponseEntity<Result> Allproject() {
        List<Project> projects = this.projectService.queryAll();
        Result result = new Result<>(200,"查询成功(!null)",projects);
        return ResponseEntity.status(200).body(result);
    }

    /**
     * 分页查询
     *
     * @param project     筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     * @PageableDefault(size = 4, sort = "id", direction = Sort.Direction.ASC)
     *
     * 此不可用
     */
    @Override
    public ResponseEntity<Result> queryByPage_limit(@RequestParam("Project") Project project, PageRequest pageRequest) {
        Page<Project> projects = this.projectService.queryByPage_limit(project, pageRequest);
        Result result = new Result<>(200,"查询成功(!null)",projects);
        return ResponseEntity.status(200).body(result);
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 页大小
     * @return 查询结果
     */
    @Override
    public ResponseEntity<Result> queryByPage(int page, int size) {
        Page<Project> projectPages = projectService.GetProjectByPage(page, size);
        Result result = new Result<>(200,"查询成功(!null)",projectPages);
        return ResponseEntity.status(200).body(result);
    }

    /**
     * 分页查询
     *
     * @param pages 分页对象
     * @return 查询结果
     */
    @Override
    public ResponseEntity<Result> queryByPage_Pages(Pages pages) {
        Pages projectPages = projectService.queryByPage(pages);
        Result result = new Result<>(200,"查询成功(!null)",projectPages);
        return ResponseEntity.status(200).body(result);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Override
    public ResponseEntity<Result> queryById(@PathVariable("id") Long id) {
        Project project = this.projectService.queryById(id);
        Result result = new Result<>(200,"查询成功(!null)",project);
        return ResponseEntity.status(200).body(result);
    }

    /**
     * 新增数据
     *
     * @param project 实体
     * @return 新增结果
     */
    @Override
    public ResponseEntity<Result> add(Project project) {
        System.currentTimeMillis();
        Date now = new Date();
        project.setCreateTime(now);
        project.setUpdateTime(now);
        Project insert = this.projectService.insert(project);
        Result result = new Result<>(200,"新增成功(!null)",insert);
        return ResponseEntity.status(200).body(result);
    }

    /**
     * 更新数据
     *
     * @param project 实体
     * @return 更新结果
     */
    @Override
    public ResponseEntity<Result> edit(Project project) {
        Project update = this.projectService.update(project);
        Result result = new Result<>(200,"更新成功(!null)",update);
        return ResponseEntity.status(200).body(result);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @Override
    public ResponseEntity<Result> deleteById(Long id) {
        boolean deleteById = this.projectService.deleteById(id);
        Result result = new Result<>(200,"成功/失败(true/false)",deleteById);
        return ResponseEntity.status(200).body(result);
    }

    /**
     * 模糊查询
     *
     * @param value
     * @return
     */
    @Override
    public ResponseEntity<Result> blurAll(String value) {
        List<Project> projects = this.projectService.searchByKeyword(value);
        Result result = new Result<>(200,"查询成功",projects);
        return ResponseEntity.status(200).body(result);
    }


}

